#include "CalcWindow.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QTextEdit>
#include <QLineEdit>
#include <QPushButton>
#include "Product.h"
#include <QMessageBox>

/* class CalcWindow definition */

CalcWindow::CalcWindow(QWidget *parent) :
        QWidget(parent)

{

    inputProduct=new QComboBox(this);
    inputProduct->setEditable("true");
    construct();

    inputAddedProduct= new QLineEdit(this);
    inputAddedProduct->setVisible(false);

    inputProductLabel = new QLabel(tr("Product"), this);
    inputWeightLabel = new QLabel(tr("Weight:"), this);
    inputWeight = new QLineEdit(this);

    outputFatLabel = new QLabel(tr("Fat:"), this);
    outputFat = new QLineEdit(this);
    outputFatLabel->setVisible(false);
    outputFat->setVisible(false);

    outputCarLabel = new QLabel(tr ("Carbohydrate:"), this);
    outputCarbohydrate = new QLineEdit(this);
    outputCarLabel->setVisible(false);
    outputCarbohydrate->setVisible(false);

    outputProteinLabel = new QLabel(tr("Protein:"), this);
    outputProtein = new QLineEdit(this);
    outputProteinLabel->setVisible(false);
    outputProtein->setVisible(false);

    outputEnergyLabel = new QLabel(tr("Energy:"), this);
    outputEnergy = new QLineEdit(this);
    outputEnergyLabel->setVisible(false);
    outputEnergy->setVisible(false);


    calculateButton = new QPushButton(tr("Calculate"), this);
    addButton = new QPushButton(tr("Add product"), this);
    saveAdded = new QPushButton(tr("Ok"), this);
    cancel = new QPushButton(tr("Cancel"), this);
    saveAdded->setVisible(false);
    cancel->setVisible(false);
    QDoubleValidator *val = new QDoubleValidator(this);
    outputFat->setValidator(val);
    outputProtein->setValidator(val);
    outputCarbohydrate->setValidator(val);
    outputEnergy->setValidator(val);
    inputWeight->setValidator(val);

    QVBoxLayout *mainLayout = new QVBoxLayout(this);




    QHBoxLayout *layout = new QHBoxLayout();
    layout->addWidget(calculateButton);
    layout->addWidget(addButton);

    QHBoxLayout *l1 = new QHBoxLayout();
    l1->addWidget(saveAdded);
    l1->addWidget(cancel);

    mainLayout->addWidget(inputProductLabel);
    mainLayout->addWidget(inputProduct);
    mainLayout->addWidget(inputAddedProduct);
    mainLayout->addWidget(inputWeightLabel);
    mainLayout->addWidget(inputWeight);
    mainLayout->addLayout(layout);
    mainLayout->addWidget(outputFatLabel);
    mainLayout->addWidget(outputFat);
    mainLayout->addWidget(outputCarLabel);
    mainLayout->addWidget(outputCarbohydrate);
    mainLayout->addWidget(outputProteinLabel);
    mainLayout->addWidget(outputProtein);
    mainLayout->addWidget(outputEnergyLabel);
    mainLayout->addWidget(outputEnergy);
    mainLayout->addLayout(l1);
    mainLayout->addStretch();
    //mainLayout->addWidget(&window);
    //mainLayout->addWidget(&addButton1);
    //window.centralWidget.setVisible(false);
   // addButton1.setVisible(false);

//outputEnergy.setVisible(false);

    connect(calculateButton, SIGNAL(clicked()),
            this, SLOT(onCalculateButtonClicked()));
    connect(addButton, SIGNAL(clicked()),
             this, SLOT(onAddButtonClicked1()));

    connect(saveAdded, SIGNAL(clicked()),
            this, SLOT(onAddButtonClicked2()));
    connect(cancel, SIGNAL(clicked()),
            this, SLOT(onCancelButton()));
  /*  connect(&addButton1, SIGNAL(clicked()),
            this, SLOT(onAddButtonClicked1()));
*/
}

void CalcWindow::onCalculateButtonClicked()
{
    saveAdded->setVisible(false);
    cancel->setVisible(false);
    outputFat->setVisible(true);
    outputProtein->setVisible(true);
    outputCarbohydrate->setVisible(true);
    outputEnergy->setVisible(true);




    outputFatLabel->setVisible(true);
    outputCarLabel->setVisible(true) ;
    outputProteinLabel->setVisible(true);
    outputEnergyLabel->setVisible(true);

    outputFat->setReadOnly(true);
    outputProtein->setReadOnly(true);
    outputCarbohydrate->setReadOnly(true);
    outputEnergy->setReadOnly(true);

    //file.open(QIODevice::ReadOnly());
    if (inputProduct->currentText() == 0)
    {
        QMessageBox::information(this, tr("Bridget"), tr("No input product."));
        return;
    }
    if (inputWeight->text().size() == 0)
    {
        QMessageBox::information(this, tr("Bridget"), tr("No input weight."));
        return;
    }

    Product *product=new Product;
    product->setName(inputProduct->currentText());
    product->setWeight(inputWeight->text().toInt());
    if (product->setPar())
    {
        outputFat->setText(QString::number(product->getFat()));
        outputProtein->setText(QString::number(product->getProtein()));
        outputCarbohydrate->setText(QString::number(product->getCarbohydrate()));
        outputEnergy->setText(QString::number(product->getEnergy()));
    }
    else
    {
        QMessageBox::information(this, tr("Bridget"), tr("This product is absent in base."));
        return;
    }


}

void CalcWindow::construct()
{
    inputProduct->clear();

    //QStringList lst;
        QFile file("baseProduct.xml");
        if (file.open(QIODevice::ReadOnly))
        {
            if (docProd.setContent(&file))
            {
                QDomElement root = docProd.documentElement();
                QDomElement child = root.firstChildElement("product");
                while (!child.isNull())
                {
                    lst.push_back(child.attribute("name", ""));
               //     inputProduct->addItem(child.attribute("name", ""));
                    child = child.nextSiblingElement("product");
                   // qDebug()<<(child.attribute("name", ""));
                }

            }

         file.close();
         }
    lst.sort();

    inputProduct->addItems(lst);
    inputProduct->clearEditText();

}

void CalcWindow::onAddButtonClicked1()
{
    calculateButton->setVisible(false);
    addButton->setVisible(false);
    outputFat->setVisible(true);
    outputProtein->setVisible(true);
    outputCarbohydrate->setVisible(true);
    outputEnergy->setVisible(true);
    outputFat->setText("");
    outputProtein->setText("");
    outputCarbohydrate->setText("");
    outputEnergy->setText("");

    outputFat->setReadOnly(false);
    outputProtein->setReadOnly(false);
    outputCarbohydrate->setReadOnly(false);
    outputEnergy->setReadOnly(false);


    outputFatLabel->setVisible(true);
    outputCarLabel->setVisible(true) ;
    outputProteinLabel->setVisible(true);
    outputEnergyLabel->setVisible(true);

    saveAdded->setVisible(true);
    cancel->setVisible(true);
    inputWeightLabel->setVisible(false);
    inputWeight->setVisible(false);
    inputAddedProduct->setVisible(true);
    inputAddedProduct->setText("");
    inputProduct->setVisible(false);


}
void CalcWindow::onAddButtonClicked2()
{
    Product *product=new Product;
    product->setName(inputAddedProduct->text());
    product->setProtein(outputProtein->text().toDouble());
    product->setFat(outputFat->text().toDouble());
    product->setCarbohydrate(outputCarbohydrate->text().toDouble());
    product->setEnergy(outputEnergy->text().toDouble());

    if (product->inBase())
    {
        QMessageBox::information(this, tr("Bridget"), tr("This product is already in the base."));
    }
    else
    {
    product->addProduct();
    lst.push_back(product->getName());
    lst.sort();
    inputProduct->clear();
    inputProduct->addItems(lst);
    inputProduct->clearEditText();


    QMessageBox::information(this, tr("Bridget"), tr("Product is added in base."));

}
}

void CalcWindow::onCancelButton()
{
    calculateButton->setVisible(true);
    addButton->setVisible(true);
    saveAdded->setVisible(false);
    cancel->setVisible(false);
    outputFat->setVisible(false);
    outputProtein->setVisible(false);
    outputCarbohydrate->setVisible(false);
    outputEnergy->setVisible(false);

    outputFatLabel->setVisible(false);
    outputCarLabel->setVisible(false) ;
    outputProteinLabel->setVisible(false);
    outputEnergyLabel->setVisible(false);
    inputWeightLabel->setVisible(true);
    inputWeight->setVisible(true);
    inputWeight->setText("");
    inputProduct->setVisible(true);
    inputProduct->clearEditText();
    inputAddedProduct->setVisible(false);

}


